package com.example.silvercare;

import com.example.server.SCSocketComm;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;

public class Setting extends PreferenceActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		// Load the preferences from an XML resource
		addPreferencesFromResource(R.layout.setting);
		
		Preference trustfriendPref = (Preference) findPreference("trustfriendPref");
		trustfriendPref.setOnPreferenceClickListener(new OnPreferenceClickListener(){

			@Override
			public boolean onPreferenceClick(Preference arg0) {
				// TODO Auto-generated method stub
				startActivity(new Intent("com.example.server.TrustFriendsActivity"));
				return false;
			}
			
		});
		
		EditTextPreference namePref = (EditTextPreference) findPreference("namePref");

		namePref.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

			@Override
			public boolean onPreferenceChange(Preference arg0, Object arg1) {
				// TODO Auto-generated method stub
				System.out.println("User name change "+arg1.toString());
				String result = UserInputCheck.nameCheck(arg1.toString());
				if(result==null)
				{
					Toast.makeText(Setting.this, "Input Invalid or Null", Toast.LENGTH_SHORT).show();
					return false;
				}
				else
				{
					((EditTextPreference)arg0).setText(result);
					//System.out.println(((EditTextPreference)arg0).getText());
					return false;
				}
			}
			
		});
		
		
		EditTextPreference emailaddressPref = (EditTextPreference) findPreference("emailaddressPref");

		emailaddressPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

			@Override
			public boolean onPreferenceChange(Preference arg0, Object arg1) {
				// TODO Auto-generated method stub
				System.out.println("Email address change "+arg1.toString());
				String result = UserInputCheck.emailaddressCheck(arg1.toString());
				if(result==null)
				{
					Toast.makeText(Setting.this, "Input Invalid or Null", Toast.LENGTH_SHORT).show();
					return false;
				}
				else
				{
					((EditTextPreference)arg0).setText(result);
					//System.out.println(((EditTextPreference)arg0).getText());
					return false;
				}
			}
			
		});
	}

}
